package com.aaa.service.impl;

import com.aaa.dao.CommentDao;
import com.aaa.entity.Comment;
import com.aaa.service.CommentService;
import com.aaa.util.DefaultMsg;
import com.aaa.util.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao;
    public TableData<Comment> page(String keyword, Integer page, Integer limit) {
        TableData<Comment> tableData=new TableData<Comment>();
        Integer offset=(page-1)*limit;
        List<Comment> data = commentDao.listPage(keyword, offset, limit);
        tableData.setData(data);
        Integer count=commentDao.count(keyword);
        tableData.setCount(count);
        return tableData;
    }

    public DefaultMsg saveOrupdate(Comment comment) {
        DefaultMsg defaultMsg = new DefaultMsg();
        Integer count;
        if (comment.getId()==null){
            count=commentDao.save(comment);
        }else {
            count=commentDao.update(comment);
        }
        if (count==0){
            defaultMsg.setSuccess(0);
            defaultMsg.setError("失败");
        }
        return defaultMsg;
    }

    public DefaultMsg delete(Integer id) {
        DefaultMsg defaultMsg = new DefaultMsg();
        Integer count = commentDao.delete(id);
        if (count==0){
            defaultMsg.setSuccess(0);
            defaultMsg.setError("失败");
        }
        return defaultMsg;
    }
}
